package com.sikaryofficial.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sikaryofficial.backend.constant.ReportBusinessTypeEnum;
import com.sikaryofficial.backend.domain.dto.req.ShieldReq;
import com.sikaryofficial.backend.domain.entity.UserShield;
import com.sikaryofficial.backend.mapper.UserShieldMapper;
import com.sikaryofficial.backend.service.IUserShieldService;
import com.sikaryofficial.backend.service.user.UserReportPubService;
import com.sikaryofficial.common.core.exception.ServiceException;
import com.sikaryofficial.common.redis.service.RedisService;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * 屏蔽Service业务层处理
 *
 * @author qinjinyuan
 * @date 2023-11-16
 */
@Service
@Slf4j
public class UserShieldServiceImpl extends ServiceImpl<UserShieldMapper, UserShield> implements IUserShieldService {
    private static final String SHIELD_FLAG = "shield";
    private static final Integer SHIELD_BUSINESS_TYPE_ARTICLE = 4;
    @Autowired
    private UserShieldMapper shieldMapper;
    @Autowired
    private UserReportPubService pubService;
    @Autowired
    private RedisService redisService;

    /**
     * @param shield
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveShield(ShieldReq shield) {
        // 0-自己的内容，不能进行屏蔽
        checkRelationObject(shield);
        // 判定该用户是否已经屏蔽过对应数据
        UserShield shieldEntity = shieldMapper.selectOne(new LambdaQueryWrapper<UserShield>()
                .eq(UserShield::getDeletedVersion, 0L)
                .eq(UserShield::getObjectId, shield.getObjectId())
                .eq(UserShield::getBusinessType, shield.getBusinessType())
                .eq(UserShield::getCreatedBy, SecurityUtils.getUserId())
                .last(" limit 1")
        );
        if (Objects.nonNull(shieldEntity)) {
            log.error("The user has already blocked the data.");
            return false;
        }
        shieldEntity = new UserShield();
        shieldEntity.setObjectId(shield.getObjectId());
        shieldEntity.setBusinessType(shield.getBusinessType());
        shieldEntity.setCreatedBy(SecurityUtils.getUserId());
        shieldEntity.setCreatedTime(new Date());
        return shieldMapper.insert(shieldEntity) > 1;
    }

    /**
     * 检测是否是自己发布的内容
     *
     * @param shield
     */
    private void checkRelationObject(ShieldReq shield) {
        ReportBusinessTypeEnum reportBusinessTypeEnum = ReportBusinessTypeEnum.getEnum(shield.getBusinessType());
        if (Objects.isNull(reportBusinessTypeEnum)) {
            throw new ServiceException("businessType is not exist");
        }
        if (Objects.isNull(SecurityUtils.getUserId())) {
            throw new ServiceException("userid is not exist");
        }
        pubService.checkRelationObject(reportBusinessTypeEnum, shield.getObjectId());
    }
}
